package controller

import (
	"net/http"

	"akexc.com/gin-template/dto"
	"akexc.com/gin-template/response"
	"akexc.com/gin-template/service"
	"akexc.com/gin-template/util"
	"github.com/gin-gonic/gin"
)

func Register(ctx *gin.Context) {
	// controller面向客户端,主要功能: 路由参数预校验,对service提供业务参数,响应客户端
	// email := ctx.PostForm("email")
	// password := ctx.PostForm("password")

	var user dto.RegisterDto

	err := ctx.Bind(&user)
	if err != nil {
		response.ResU(ctx, 422, 4022, "参数错误", gin.H{"data": err})
		return
	}

	email := user.Email
	password := user.Password

	//密码过短
	if len(password) < 6 {
		response.Fail(ctx, response.PasswordCheck, nil)
		return
	}
	// 邮箱格式错误
	if !util.CheckEmail(email) {
		response.Fail(ctx, response.EmailFormatCheck, nil)
		return
	}

	res := service.Register(user)
	response.AllResponse(ctx, res)

}

func Login(ctx *gin.Context) {
	response.ResU(ctx, http.StatusOK, 200, "user routes", nil)
}
